package com.newworld.dmsapp2;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.newworld.dmsapp2.util.SysApplication;

/**
 * @author Administrator
 * 
 */
@SuppressLint("ResourceAsColor")
public abstract class BaseActivity extends FragmentActivity {

	/**
	 * 屏幕的宽度、高度、密度
	 */
	protected int mScreenWidth;
	protected int mScreenHeight;
	protected float mDensity;

	protected List<AsyncTask<Void, Void, Boolean>> mAsyncTasks = new ArrayList<AsyncTask<Void, Void, Boolean>>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		DisplayMetrics metric = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metric);
		mScreenWidth = metric.widthPixels;
		mScreenHeight = metric.heightPixels;
		mDensity = metric.density;
	}

	@Override
	protected void onDestroy() {
		clearAsyncTask();
		super.onDestroy();
	}

	/**
	 * @Description 初始化视图
	 * @param 无
	 * @return void
	 * @author 左洁
	 */
	protected abstract void initViews();

	/** 初始化事件 **/

	/**
	 * @Description 初始化事件
	 * @param 无
	 * @return void
	 * @author 左洁
	 */
	protected abstract void initEvents();

	protected void putAsyncTask(AsyncTask<Void, Void, Boolean> asyncTask) {
		mAsyncTasks.add(asyncTask.execute());
	}

	protected void clearAsyncTask() {
		Iterator<AsyncTask<Void, Void, Boolean>> iterator = mAsyncTasks
				.iterator();
		while (iterator.hasNext()) {
			AsyncTask<Void, Void, Boolean> asyncTask = iterator.next();
			if (asyncTask != null && !asyncTask.isCancelled()) {
				asyncTask.cancel(true);
			}
		}
		mAsyncTasks.clear();
	}

	/**
	 * @Description 设置titlebar标题栏（有左侧）
	 * @param Context
	 *            context
	 * @param String
	 *            titlestr
	 * @param Class
	 *            second
	 * @return void
	 * @time 2015-6-14上午11:53:35
	 * @author 左洁
	 */
	@SuppressWarnings("rawtypes")
	protected void setTitlebarLeft(final Context context, String titlestr,
			final Class second) {
		SysApplication.getInstance().addActivity((Activity) context);
		TextView titleView = (TextView) findViewById(R.id.titlebar_left_center_title);
		titleView.setText(titlestr);
		LinearLayout blayout = (LinearLayout) findViewById(R.id.titlebar_left_left_layout);
		blayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, second);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
				startActivity(intent);
				((Activity) context).finish();
			}
		});
	}

	/*
	 * protected void setTitlebarBoth(final Context context,String
	 * titlestr,final Class second,Drawable rightid){
	 * SysApplication.getInstance().addActivity((Activity) context); TextView
	 * titleView = (TextView)findViewById(R.id.titlebar_both_center_title);
	 * TextView righttx = (TextView)findViewById(R.id.titlebar_both_right_text);
	 * titleView.setText(titlestr); righttx.setBackgroundDrawable(rightid);
	 * LinearLayout leftlayout=(LinearLayout)
	 * findViewById(R.id.titlebar_both_left_layout);
	 * leftlayout.setOnClickListener(new OnClickListener() {
	 * 
	 * @Override public void onClick(View v) { Intent intent = new
	 * Intent(context,second); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	 * intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(intent);
	 * ((Activity) context).finish(); } }); LinearLayout
	 * rightlayout=(LinearLayout) findViewById(R.id.titlebar_both_right_layout);
	 * rightlayout.setOnClickListener(new OnClickListener() {
	 * 
	 * @Override public void onClick(View v) { rightListener(); } }); }
	 */

	protected void rightListener() {
	}

	protected void setTitlebarNone(final Context context, String titlestr) {
		SysApplication.getInstance().addActivity((Activity) context);
		TextView titleView = (TextView) findViewById(R.id.titlebar_none_center_title);
		titleView.setText(titlestr);
	}

	/**
	 * @Description 设置想到时间轴(2个节点)
	 * @param int position
	 * @param String
	 *            tx1
	 * @param String
	 *            tx2
	 * @return void
	 * @time 2015-6-14下午9:09:08
	 * @author 左洁
	 */
	protected void setGuideTimeLine_2stage(int position, String tx1,
			String tx2, OnClickListener listenera) {
		TextView point1 = (TextView) findViewById(R.id.guide_point2_1);
		TextView point2 = (TextView) findViewById(R.id.guide_point2_2);
		switch (position) {
		case 1:
			point1.setTextColor(this.getResources().getColor(
					R.color.nw_tl_tx_green));
			point1.setOnClickListener(listenera);
			break;
		case 2:
			point2.setTextColor(this.getResources().getColor(
					R.color.nw_tl_tx_green));
			point2.setOnClickListener(listenera);
			break;
		default:
			break;
		}
		point1.setText(tx1);
		point2.setText(tx2);
	}

	/**
	 * @Description 设置想到时间轴(3个节点)
	 * @param int position
	 * @param String
	 *            tx1
	 * @param String
	 *            tx2
	 * @param String
	 *            tx3
	 * @return void
	 * @time 2015-6-14下午9:09:08
	 * @author 左洁
	 */
	protected void setGuideTimeLine_3stage(String tx1, String tx2, String tx3,
			OnClickListener listenera, OnClickListener listenerb) {
		TextView point1 = (TextView) findViewById(R.id.guide_point3_1);
		TextView point2 = (TextView) findViewById(R.id.guide_point3_2);
		TextView point3 = (TextView) findViewById(R.id.guide_point3_3);
		/*
		 * switch(position){ case 1:
		 * point1.setTextColor(this.getResources().getColor
		 * (R.color.nw_tl_tx_green)); point2.setOnClickListener(listenera);
		 * point3.setOnClickListener(listenerb); break; case 2:
		 * point2.setTextColor
		 * (this.getResources().getColor(R.color.nw_tl_tx_green));
		 * point1.setOnClickListener(listenera);
		 * point3.setOnClickListener(listenerb); break; case 3:
		 * point3.setTextColor
		 * (this.getResources().getColor(R.color.nw_tl_tx_green));
		 * point1.setOnClickListener(listenera);
		 * point2.setOnClickListener(listenerb); break; default: break; }
		 */
		point1.setText(tx1);
		point2.setText(tx2);
		point3.setText(tx3);

	}

	/**
	 * @Description 设置想到时间轴(4个节点)
	 * @param int position
	 * @param String
	 *            tx1
	 * @param String
	 *            tx2
	 * @param String
	 *            tx3
	 * @param String
	 *            tx4
	 * @return void
	 * @time 2015-6-14下午9:09:08
	 * @author 左洁
	 */
	protected void setGuideTimeLine_4stage(int position, String tx1,
			String tx2, String tx3, String tx4, OnClickListener listenera,
			OnClickListener listenerb, OnClickListener listenerc) {
		TextView point1 = (TextView) findViewById(R.id.guide_point4_1);
		TextView point2 = (TextView) findViewById(R.id.guide_point4_2);
		TextView point3 = (TextView) findViewById(R.id.guide_point4_3);
		TextView point4 = (TextView) findViewById(R.id.guide_point4_4);
		switch (position) {
		case 1:
			point1.setTextColor(this.getResources().getColor(
					R.color.nw_tl_tx_green));
			point2.setOnClickListener(listenera);
			point3.setOnClickListener(listenerb);
			point4.setOnClickListener(listenerc);
			break;
		case 2:
			point2.setTextColor(this.getResources().getColor(
					R.color.nw_tl_tx_green));
			point1.setOnClickListener(listenera);
			point3.setOnClickListener(listenerb);
			point4.setOnClickListener(listenerc);
			break;
		case 3:
			point3.setTextColor(this.getResources().getColor(
					R.color.nw_tl_tx_green));
			point1.setOnClickListener(listenera);
			point2.setOnClickListener(listenerb);
			point4.setOnClickListener(listenerc);
			break;
		case 4:
			point4.setTextColor(this.getResources().getColor(
					R.color.nw_tl_tx_green));
			point1.setOnClickListener(listenera);
			point2.setOnClickListener(listenerb);
			point3.setOnClickListener(listenerc);
			break;
		default:
			break;
		}
		point1.setText(tx1);
		point2.setText(tx2);
		point3.setText(tx3);
		point4.setText(tx4);

	}

	/**
	 * @Description 短暂显示Toast提示(来自res)
	 * @param int resId
	 * @return void
	 * @author 左洁
	 */
	protected void showShortToast(int resId) {
		Toast.makeText(this, getString(resId), Toast.LENGTH_SHORT).show();
	}

	/**
	 * @Description 短暂显示Toast提示(来自String)
	 * @param String
	 *            text
	 * @return void
	 * @author 左洁
	 */
	protected void showShortToast(String text) {
		Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
	}

	/**
	 * @Description 长时间显示Toast提示(来自res)
	 * @param int resId
	 * @return void
	 * @author 左洁
	 */
	protected void showLongToast(int resId) {
		Toast.makeText(this, getString(resId), Toast.LENGTH_LONG).show();
	}

	/**
	 * @Description 长时间显示Toast提示(来自String)
	 * @param String
	 *            text
	 * @return void
	 * @author 左洁
	 */
	protected void showLongToast(String text) {
		Toast.makeText(this, text, Toast.LENGTH_LONG).show();
	}

	/**
	 * @Description Debug输出Log日志
	 * @param String
	 *            tag
	 * @param String
	 *            msg
	 * @return void
	 * @author 左洁
	 */
	protected void showLogDebug(String tag, String msg) {
		Log.d(tag, msg);
	}

	/** Error输出Log日志 **/

	/**
	 * @Description TODO
	 * @param @param tag
	 * @param @param msg
	 * @return void
	 * @author
	 */
	protected void showLogError(String tag, String msg) {
		Log.e(tag, msg);
	}

	/**
	 * @Description 通过Class跳转界面
	 * @param Class
	 *            <?> cls
	 * @return void
	 * @author 左洁
	 */
	protected void startActivity(Class<?> cls) {
		startActivity(cls, null);
	}

	/**
	 * @Description 两个界面之间跳转，并结束当前界面
	 * 
	 * @param Context
	 *            first
	 * @param Class
	 *            second
	 * @return void
	 * @time 2015-6-13下午4:05:36
	 * @author 左洁
	 */
	@SuppressWarnings("rawtypes")
	protected void intentActivity(Context first, Class second) {
		Intent intent = new Intent(first, second);
		startActivity(intent);
		((Activity) first).finish();
	}

	/**
	 * 
	 * @Description titlebar返回跳转
	 * @param Context
	 *            first
	 * @param Class
	 *            second
	 * @return void
	 * @time 2015-6-13下午4:05:36
	 * @author 左洁
	 */
	@SuppressWarnings("rawtypes")
	protected void backIntentActivity(Context first, Class second) {
		Intent intent = new Intent(first, second);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		startActivity(intent);
		((Activity) first).finish();
	}

	/**
	 * @Description 含有Bundle通过Class跳转界面
	 * @param @param cls
	 * @param @param bundle
	 * @return void
	 * @time 2015-6-13下午4:05:36
	 * @author
	 */
	protected void startActivity(Class<?> cls, Bundle bundle) {
		Intent intent = new Intent();
		intent.setClass(this, cls);
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		startActivity(intent);
	}

	/**
	 * @Description 通过Action跳转界面
	 * @param String
	 *            action
	 * @return void
	 * @time 2015-6-13下午4:05:36
	 * @author 左洁
	 */
	protected void startActivity(String action) {
		startActivity(action, null);
	}

	/**
	 * @Description 含有Bundle通过Action跳转界面
	 * @param String
	 *            action
	 * @param Bundle
	 *            bundle
	 * @return void
	 * @time 2015-6-13下午4:05:36
	 * @author 左洁
	 */
	protected void startActivity(String action, Bundle bundle) {
		Intent intent = new Intent();
		intent.setAction(action);
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		startActivity(intent);
	}

	/**
	 * @Description 含有标题和内容的对话框
	 * @param String
	 *            title
	 * @param String
	 *            message
	 * @param @return
	 * @return AlertDialog
	 * @time 2015-6-13下午4:05:36
	 * @author 左洁
	 */
	protected AlertDialog showAlertDialog(String title, String message) {
		AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle(title)
				.setMessage(message).show();
		return alertDialog;
	}

	/** 含有标题、内容、两个按钮的对话框 **/

	/**
	 * @Description 含有标题、内容、两个按钮的对话框
	 * @param String
	 *            title
	 * @param String
	 *            message
	 * @param String
	 *            positiveText
	 * @param DialogInterface
	 *            .OnClickListener onPositiveClickListener
	 * @param String
	 *            negativeText
	 * @param DialogInterface
	 *            .OnClickListener onNegativeClickListener
	 * @return AlertDialog
	 * @time 2015-6-13下午4:05:36
	 * @author
	 */
	protected AlertDialog showAlertDialog(String title, String message,
			String positiveText,
			DialogInterface.OnClickListener onPositiveClickListener,
			String negativeText,
			DialogInterface.OnClickListener onNegativeClickListener) {
		AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle(title)
				.setMessage(message)
				.setPositiveButton(positiveText, onPositiveClickListener)
				.setNegativeButton(negativeText, onNegativeClickListener)
				.show();
		return alertDialog;
	}

	/**
	 * @Description 含有标题、内容、图标、两个按钮的对话框
	 * @param String
	 *            title
	 * @param String
	 *            message
	 * @param icon
	 *            icon
	 * @param String
	 *            positiveText
	 * @param DialogInterface
	 *            .OnClickListener onPositiveClickListener
	 * @param String
	 *            negativeText
	 * @param DialogInterface
	 *            .OnClickListener onNegativeClickListener
	 * @return AlertDialog
	 * @time 2015-6-13下午4:05:36
	 * @author 左洁
	 */

	protected AlertDialog showAlertDialog(String title, String message,
			int icon, String positiveText,
			DialogInterface.OnClickListener onPositiveClickListener,
			String negativeText,
			DialogInterface.OnClickListener onNegativeClickListener) {
		AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle(title)
				.setMessage(message).setIcon(icon)
				.setPositiveButton(positiveText, onPositiveClickListener)
				.setNegativeButton(negativeText, onNegativeClickListener)
				.show();
		return alertDialog;

	}

	protected AlertDialog showAlertDialog(String title, String message,
			int icon, String positiveText, String negativeText) {
		AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle(title)
				.setMessage(message).setIcon(icon)
				.setPositiveButton(positiveText, onPositiveClickListener)
				.setNegativeButton(negativeText, onNegativeClickListener)
				.show();
		return alertDialog;
	}

	DialogInterface.OnClickListener onPositiveClickListener = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface arg0, int arg1) {
			// TODO Auto-generated method stub

		}
	};

	DialogInterface.OnClickListener onNegativeClickListener = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface arg0, int arg1) {
			// TODO Auto-generated method stub

		}
	};

	protected void showListDialog(View source, Context context, String[] items,
			DialogInterface.OnClickListener onItemClickListener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context)
				.setItems(items, onItemClickListener);
		// 创建对话框
		AlertDialog ad = builder.create();
		// 显示对话框
		ad.show();
	}

	/**
	 * @Description 默认退出
	 * @param
	 * @return void
	 * @time 2015-6-13下午4:10:46
	 * @author 左洁
	 */
	protected void defaultFinish() {
		super.finish();
	}
}
